"""
解题思路

这道题是一个非常有意思的题目，

"""


class Solution:
    def maxSlidingWindow(self, nums, k: int):
        ans = []
        deque = collections.deque()
        for i in range(len(nums)):
            # 删除超过长度的元素:
            if deque and i - deque[0] >= k:
                deque.popleft()
            # 删除前方非最大值
            while deque and nums[deque[-1]] <= nums[i]:
                deque.pop()
            # 添加元素
            deque.append(i)
            if i >= k - 1:
                ans.append(nums[deque[0]])
        return ans